package com.projet.whattowatch.activity;

import java.util.ArrayList;

import com.projet.whattowatch.R;
import com.projet.whattowatch.adapter.CommentAdapter;
import com.projet.whattowatch.objet.Avis;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.LinearLayout;

public class MovieFragmentResume extends Fragment
{
	WebView mWebView;
	LinearLayout commentLinear;
	
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
            View rootView = inflater.inflate(R.layout.movie_synopsis_layout, container, false);
            
            mWebView = (WebView) rootView.findViewById(R.id.webView);
            commentLinear = (LinearLayout) rootView.findViewById(R.id.listViewComment);
            
            ArrayList<String> nameUserList = new ArrayList<String>();
            ArrayList<String> ratingList = new ArrayList<String>();
            ArrayList<String> commentList = new ArrayList<String>();
            
            for(int i = 0; i < MovieDetailActivity.film.getListAvis().size(); i++)
            {
            	Avis avis = MovieDetailActivity.film.getListAvis().get(i);
            	String nameUser = avis.getUtilisateur();
            	String rating = String.valueOf(avis.getRating());
            	String comment = avis.getCommentaire();
            	for(int j = 0; j < MainActivity.mLibUtilisateur.getlistUtilisateur().size(); j++)
            	{
            		if((MainActivity.mLibUtilisateur.getlistUtilisateur().get(j).getAdresseMail()).equals(nameUser))
            		{
            			nameUser = MainActivity.mLibUtilisateur.getlistUtilisateur().get(j).getPrenom();
            		}
            	}
            	nameUserList.add(nameUser);
            	ratingList.add(rating);
            	commentList.add(comment);
            }
            
            CommentAdapter commentAdapter = new CommentAdapter(getActivity(), nameUserList, ratingList, commentList);
            
            final int commentCount = commentAdapter.getCount();
            for (int i = 0; i < commentCount; i++)
            {
              View item = commentAdapter.getView(i, null, null);
              commentLinear.addView(item);
            }
            
            String text = "<html><body>"
                         + "<p align=\"justify\">"                
                         + MovieDetailActivity.film.getResume() 
                         + "</p> "
                         + "</body></html>";
                     
            mWebView.loadData(text, "text/html", "utf-8");
            
            return rootView;
    }
}